//
//  ClubSettingViewController.swift
//  77Club
//
//  Created by Apple on 2024/9/12.
//

import UIKit

class ClubSettingViewController: ClubBaseViewController {

    private let backButton = c_make_ex(UIButton.self)
        .image("Common_blackBack").end()
    private let titleLabel = c_make_ex(UILabel.self)
        .text("设置中心").color(.black).font(16.font_semiBold)
        .alignment(.center).end()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.register(ClubSettingTableViewCell.self,ClubGame77ClubCell.self,ClubSettingOutCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        return tableView
    }()
    var fromMineVC = false //是否从个人主页进入
    
    private var settingData = [[ClubSettingModel]]()
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        loadData()
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.subviews{
            backButton
            titleLabel
            tableView
        }
        
        backButton.leading(0).top(.statusHeight).width(56).height(44)
        titleLabel.centerHorizontally()
        titleLabel.CenterY == backButton.CenterY
        tableView.leading(0).trailing(0).top(.navi_height).bottom(.safe_bottom)
        
        backButton.addTarget(self, action: #selector(clickBackButton), for: .touchUpInside)
    }
    
    private func loadData(){
        if let uid = UserDefaultsManager.getString(forKey: .uid), uid.count > 0 {
            
            settingData = [
            [.account, .normal, .privacy, .alert],
            [.policy, .terms],
            [.link, .service, .time],
            [.logout]]
        }else{
            
            settingData = [
            [.account, .normal, .privacy, .alert],
            [.policy, .terms],
            [.link, .service, .time]]
        }
        tableView.reloadData()
    }
    

}

extension ClubSettingViewController: UITableViewDelegate, UITableViewDataSource {
    
    func numberOfSections(in tableView: UITableView) -> Int {
        settingData.count
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return 1
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        if indexPath.section == 2 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ClubGame77ClubCell", for: indexPath) as! ClubGame77ClubCell
            return cell
        }else if indexPath.section == 3 {
            let cell = tableView.dequeueReusableCell(withIdentifier: "ClubSettingOutCell", for: indexPath) as! ClubSettingOutCell
            cell.model = settingData[indexPath.section][indexPath.row]
            return cell
        }
        let cell = tableView.dequeueReusableCell(withIdentifier: "ClubSettingTableViewCell", for: indexPath) as! ClubSettingTableViewCell
        cell.models = settingData[indexPath.section]
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        
        if indexPath.section == 2 {

            return 282.0.Scale
        }else if indexPath.section == 3 {
            
            return 275.0.Scale
        }
        let count = settingData[indexPath.section].count
        return Double(count) * 48.0.Scale + 12.0.Scale
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        if indexPath.section == 3 {
            switch settingData[indexPath.section][indexPath.row] {
            case .logout:
                
                UserDefaultsManager.clearUserDefaults()
                let loginVC = ClubLoginViewController()
                loginVC.fromMineVC = fromMineVC
                self.navigationController?.pushViewController(loginVC, animated: true)
                break
            default:
                return
            }
        }
    }
    
}
